Ext.define('Admin.view.decMakConsultation.KnowledgeBaseController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.knowledgeBase',
    init: function() {
        console.log('知识库初始化');
    },

    //状态管理
    state: {
        // refs: this.getReferences(), //所有的references的集合
    },

    //专家库搜索
    expertSearch: function() {
        var refs = this.getReferences();
        var expertName = refs.expertName.getValue(); //专家领域
        var expertGrade = refs.expertGrade.getValue(); //专家职称
        var grid = refs.expertGrid;
        var params = {
            name: expertName,
            grade: expertGrade,
            river: config.RIVER
        };
        grid.getStore().getProxy().extraParams = params;
        grid.getStore().reload();
    },

    updatelist: function(Reference) {
        var that = this;
        var combo = that.lookupReference(Reference);
        var url = config.URL;
        var rep = 'name';
        switch (Reference) {
            case 'expertName':
                url = url + 'szy/dispatch/meeting/ddsSExp/list';
                rep = 'name';
                break;
            case 'expertGrade':
                url = url + 'szy/dispatch/meeting/ddsSExp/list';
                rep = 'grade';
                break;
            case 'warnPlanTitle':
                url = url + 'szy/dispatch/meeting/ddsSKno/list';
                rep = 'title';
                break;
            case 'historyCaseLoc':
                url = url + 'szy/dispatch/meeting/ddsSCase/list';
                rep = 'loc';
                break;
            default:
                url = url + 'szy/dispatch/meeting/ddsSExp/list';
                rep = 'name';
                break;
        }
        var store = Ext.create("Ext.data.Store", {
            autoLoad: true,
            proxy: {
                type: "ajax",
                extraParams: { river: config.RIVER },
                url: url,
                reader: {
                    type: "json",
                    rootProperty: "list",
                }
            },
            listeners: {
                load: function() {
                    var k, repeat = [],
                        state = {};
                    this.each(function(r) {
                        k = r.get(rep);
                        if (state[k]) repeat.push(r);
                        else state[k] = true;
                    });
                    this.remove(repeat);
                }
            }
        });
        combo.setStore(store);
    },

    //删除专家库
    expertDel: function(grid, rowIndex, colIndex) {
        var _that = this;
        var code = grid.getStore().getAt(rowIndex).get('code');
        Ext.Msg.confirm("警告", "确定要删除吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + '/szy/dispatch/meeting/ddsSExp/delete',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            grid.store.load();
                            _that.updatelist('expertName');
                            _that.updatelist('expertGrade');
                            Ext.Msg.alert('系统提示', "删除成功");
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { code: code, river: config.RIVER },
                });
            }
        });
    },

    //专家新增
    expertAdd: function() {
        var _that = this;
        // var win = Ext.ComponentQuery.query('#expertAddWindow')[0];
        var win = _that.lookupReference('expertAddWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '新增专家',
                width: 530,
                layout: 'fit',
                //				modal: true,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'expertAddWindow',
                buttons: [{
                        text: '保存',
                        handler: function() {
                            var form = _that.lookupReference('expertAddForm').getForm();
                            if (form.isValid()) {
                                var modal = this.up('window');
                                var expertGrid = _that.lookupReference('expertGrid');
                                var store = expertGrid.getStore();
                                var values = form.getValues();
                                values.river = config.RIVER;
                                Ext.Ajax.request({
                                    url: config.URL + '/szy/dispatch/meeting/ddsSExp/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        Ext.MessageBox.hide();
                                        if (result.code === 1) {
                                            Ext.Msg.alert('系统提示', "保存成功");
                                            store.reload();
                                            modal.hide();
                                            _that.updatelist('expertName');
                                            _that.updatelist('expertGrade');
                                        } else {
                                            Ext.Msg.alert("系统提示", result.msg);
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }
                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            modal.hide();
                        }
                    },
                ],
                items: {
                    xtype: 'expertForm',
                    border: false,
                    reference: 'expertAddForm'
                }
            });
            this.getView().add(win);
            // Ext.ComponentQuery.query('#mainView')[0].add(win);
        }
        win.show();
    },

    //修改专家库
    expertUpdata: function(grid, rowIndex, colIndex) {
        var _that = this;
        var rec = grid.getStore().getAt(rowIndex);
        var win = this.lookupReference('expertUpdataWindow');
        var saveUpdata = function() {
            var form = _that.lookupReference('expertUpdataForm').getForm();
            if (form.isValid()) {
                var modal = this.up('window');
                var emergencyIncidentGrid = _that.lookupReference('expertGrid');
                var store = emergencyIncidentGrid.getStore();
                var values = form.getValues();
                values.river = config.RIVER;
                Ext.Ajax.request({
                    url: config.URL + '/szy/dispatch/meeting/ddsSExp/update',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            Ext.Msg.alert('系统提示', "修改成功");
                            store.reload();
                            modal.hide();
                            _that.updatelist('expertName');
                            _that.updatelist('expertGrade');
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: values
                });
            } else {
                Ext.Msg.alert('提示', '表单填写不合法！');
            }
        };
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '修改专家信息',
                width: 530,
                layout: 'fit',
                //				modal: true,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'expertUpdataWindow',
                buttons: [{
                        text: '保存',
                        handler: saveUpdata
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            _that.lookupReference('expertUpdataForm').getForm().reset();
                            modal.hide();
                        }
                    },

                ],
                items: {
                    xtype: 'expertForm',
                    border: false,
                    reference: 'expertUpdataForm'
                }
            });
            this.getView().add(win);
        }
        win.show();
        this.lookupReference('expertUpdataForm').getForm().reset();
        this.lookupReference('expertUpdataForm').getForm().setValues(rec.data);
    },


    //应急预案搜索
    warnPlanSearch: function() {
        var refs = this.getReferences();
        var warnPlanTitle = refs.warnPlanTitle.getValue(); //title
        var warnPlanType = refs.warnPlanType.getValue(); //type
        var grid = refs.warnPlanGrid;
        var params = {
            title: warnPlanTitle,
            knoType: warnPlanType,
            columnid: 1,
            river: config.RIVER
        };
        grid.getStore().getProxy().extraParams = params;
        grid.getStore().reload();
    },


    //删除应急预案
    warnPlanDel: function(grid, rowIndex, colIndex) {
        var _that = this;
        var knoId = grid.getStore().getAt(rowIndex).get('knoId');
        Ext.Msg.confirm("警告", "确定要删除吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + '/szy/dispatch/meeting/ddsSKno/delete',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            grid.store.load();
                            _that.updatelist('warnPlanTitle');
                            Ext.Msg.alert('系统提示', "删除成功");
                        } else {
                            Ext.Msg.alert("系统提示", '删除失败');
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { knoId: knoId, columnid: 1, river: config.RIVER }
                });
            }
        });
    },

    //应急预案新增
    warnPlanAdd: function() {
        var _that = this;
        var win = _that.lookupReference('warnPlanAddWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '新增应急预案',
                width: 530,
                layout: 'fit',
                //				modal: true,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'warnPlanAddWindow',
                buttons: [

                    {
                        text: '保存',
                        handler: function() {
                            var form = _that.lookupReference('warnPlanAddForm').getForm();
                            if (form.isValid()) {
                                var modal = this.up('window');
                                var warnPlanGrid = _that.lookupReference('warnPlanGrid');
                                var store = warnPlanGrid.getStore();
                                var values = form.getValues();
                                values.river = config.RIVER;
                                values.columnid = 1;
                                Ext.Ajax.request({
                                    url: config.URL + '/szy/dispatch/meeting/ddsSKno/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        Ext.MessageBox.hide();
                                        if (result.code === 1) {
                                            Ext.Msg.alert('系统提示', "保存成功");
                                            store.reload();
                                            modal.hide();
                                            _that.updatelist('warnPlanTitle');
                                        } else {
                                            Ext.Msg.alert("系统提示", result.msg);
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }
                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            modal.hide();
                        }
                    },
                ],
                items: {
                    xtype: 'warnPlanForm',
                    border: false,
                    reference: 'warnPlanAddForm'
                }
            });
            this.getView().add(win);
        }
        win.show();
    },

    //修改应急预案
    warnPlanUpdata: function(grid, rowIndex, colIndex) {
        var rec = grid.getStore().getAt(rowIndex);
        rec.data.dt = rec.data.dt ? new Date(rec.data.dt) : "";
        console.log('rec', rec);
        var _that = this;
        var win = this.lookupReference('warnPlanUpdataWindow');
        var saveUpdata = function() {
            var form = _that.lookupReference('warnPlanUpdataForm').getForm();
            if (form.isValid()) {
                var modal = this.up('window');
                var warnPlanGrid = _that.lookupReference('warnPlanGrid');
                var store = warnPlanGrid.getStore();
                var values = form.getValues();
                values.river = config.RIVER;
                values.columnid = 1;
                Ext.Ajax.request({
                    url: config.URL + '/szy/dispatch/meeting/ddsSKno/update',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            Ext.Msg.alert('系统提示', "修改成功");
                            store.reload();
                            modal.hide();
                            _that.updatelist('warnPlanTitle');

                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: values
                });
            } else {
                Ext.Msg.alert('提示', '表单填写不合法！');
            }
        };
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '修改应急预案',
                width: 530,
                layout: 'fit',
                //				modal: true,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'warnPlanUpdataWindow',
                buttons: [{
                        text: '保存',
                        handler: saveUpdata
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            _that.lookupReference('warnPlanUpdataForm').getForm().reset();
                            modal.hide();
                        }
                    },

                ],
                items: {
                    xtype: 'warnPlanForm',
                    border: false,
                    reference: 'warnPlanUpdataForm'
                }
            });
            this.getView().add(win);
        }
        win.show();
        this.lookupReference('warnPlanUpdataForm').getForm().reset();
        this.lookupReference('warnPlanUpdataForm').getForm().setValues(rec.data);
    },

    /**
     * 历史案例搜索
     */
    historyCaseSearch: function() {
        var refs = this.getReferences();
        var historyCaseLoc = refs.historyCaseLoc.getValue(); //地点
        var grid = refs.historyCaseGrid;
        var params = {
            loc: historyCaseLoc,
            river: config.RIVER
        };
        grid.getStore().getProxy().extraParams = params;
        grid.getStore().reload();
    },

    /**
     * 删除历史案例
     */
    historyCaseDel: function(grid, rowIndex, colIndex) {
        var _that = this;
        var id = grid.getStore().getAt(rowIndex).get('id');
        Ext.Msg.confirm("警告", "确定要删除吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + '/szy/dispatch/meeting/ddsSCase/delete',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            grid.store.load();
                            _that.updatelist('historyCaseLoc');
                            Ext.Msg.alert('系统提示', "删除成功");
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { id: id, river: config.RIVER }
                });
            }
        });
    },

    /**
     * 新增历史案例
     */
    historyCaseAdd: function() {
        var _that = this;
        var win = _that.lookupReference('historyCaseAddWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '新增历史案例',
                width: 530,
                layout: 'fit',
                //				modal: true,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'historyCaseAddWindow',
                buttons: [

                    {
                        text: '保存',
                        handler: function() {
                            var form = _that.lookupReference('historyCaseAddForm').getForm();
                            if (form.isValid()) {
                                var modal = this.up('window');
                                var historyCaseGrid = _that.lookupReference('historyCaseGrid');
                                var store = historyCaseGrid.getStore();
                                var values = form.getValues();
                                values.river = config.RIVER;
                                Ext.Ajax.request({
                                    url: config.URL + '/szy/dispatch/meeting/ddsSCase/save',
                                    method: "post",
                                    success: function(response, opts) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        Ext.MessageBox.hide();
                                        if (result.code === 1) {
                                            Ext.Msg.alert('系统提示', "保存成功");
                                            store.reload();
                                            modal.hide();
                                            _that.updatelist('historyCaseLoc');
                                        } else {
                                            Ext.Msg.alert("系统提示", result.msg);
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert('系统提示', '系统出错！');
                                    },
                                    params: values
                                });
                            } else {
                                Ext.Msg.alert('提示', '表单填写不合法！');
                            }
                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            modal.hide();
                        }
                    },
                ],
                items: {
                    xtype: 'historyCaseForm',
                    border: false,
                    reference: 'historyCaseAddForm'
                }
            });
            this.getView().add(win);
        }
        win.show();
    },

    /**
     * 修改历史案例
     */
    historyCaseUpdata: function(grid, rowIndex, colIndex) {
        var rec = grid.getStore().getAt(rowIndex);
        var _that = this;
        var win = this.lookupReference('historyCaseUpdataWindow');
        var saveUpdata = function() {
            var form = _that.lookupReference('historyCaseUpdataForm').getForm();
            if (form.isValid()) {
                var modal = this.up('window');
                var historyCaseGrid = _that.lookupReference('historyCaseGrid');
                var store = historyCaseGrid.getStore();
                var values = form.getValues();
                values.river = config.RIVER;
                values.columnid = 1;
                Ext.Ajax.request({
                    url: config.URL + '/szy/dispatch/meeting/ddsSCase/update',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            Ext.Msg.alert('系统提示', "修改成功");
                            store.reload();
                            modal.hide();
                            _that.updatelist('historyCaseLoc');
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: values
                });
            } else {
                Ext.Msg.alert('提示', '表单填写不合法！');
            }
        };
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '修改历史案例',
                width: 530,
                layout: 'fit',
                //				modal: true,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'historyCaseUpdataWindow',
                buttons: [{
                        text: '保存',
                        handler: saveUpdata
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            _that.lookupReference('historyCaseUpdataForm').getForm().reset();
                            modal.hide();
                        }
                    },

                ],
                items: {
                    xtype: 'historyCaseForm',
                    border: false,
                    reference: 'historyCaseUpdataForm'
                }
            });
            this.getView().add(win);
        }
        win.show();
        this.lookupReference('historyCaseUpdataForm').getForm().reset();
        this.lookupReference('historyCaseUpdataForm').getForm().setValues(rec.data);
    }
});